local hx__shuangzhen = fk.CreateSkill {
  name = "hx__shuangzhen",
}

hx__shuangzhen:addEffect("active", {
  anim_type = "support",
  can_use = function(self, player)
    return player.phase == Player.Play
  end,
  card_num = 0,
  target_filter = function(self, player, to_select, selected)
    if player:getMark("hx__shuangzhen_count") < 2 then
      if player:getMark("@hx__shuangzhen_mark") == 0 then
        return #selected == 0
      else
        return #selected == 0 and to_select == player and player:getMark("hx__shuangzhen2") > 0
      end
    end
  end,
  target_num = 1,
  prompt = function(self, player, selected, selected_cards)
		if player:getMark("@hx__shuangzhen_mark") == 0 then
			return "hx__shuangzhen_prompt1"
		else
			return "hx__shuangzhen_prompt2"
		end
    end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    room:addPlayerMark(player, "hx__shuangzhen_can", 1)
    room:addPlayerMark(player, "hx__shuangzhen_count", 1)
    if player:getMark("@hx__shuangzhen_mark") == 0 then
      room:addPlayerMark(target, "@hx__shuangzhen_mark", 1)
      room:addPlayerMark(player, "@hx__shuangzhen_mark", 1)
      room:recover{who = player, num = 1, reason = hx__shuangzhen.name}
      room:recover{who = target, num = 1, reason = hx__shuangzhen.name}
      room:changeShield(player,1)
      room:changeShield(target,1)
      room:addPlayerMark(player, "hx__shuangzhen2", 1)
    else
      if player:getMark("hx__shuangzhen2") > 0 then
        for _, p in ipairs(room:getAlivePlayers()) do
          if p:getMark("@hx__shuangzhen_mark") > 0 then
            room:drawCards(p,2,hx__shuangzhen.name)
            room:recover{who = p, num = 1, reason = hx__shuangzhen.name}
            room:setPlayerMark(p, "@hx__shuangzhen_mark", 0)
          end
        end
        room:setPlayerMark(player, "hx__shuangzhen2", 0)
      end
    end
  end,
})

hx__shuangzhen:addEffect(fk.CardUseFinished, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(hx__shuangzhen.name) and player:getMark("hx__shuangzhen_count") < 2 then
      if player:getMark("hx__shuangzhen2") == 0 then
        if player:getMark("@hx__shuangzhen_mark") == 0 then
          if target == player then
            return true
          end
        end
      else
        if target == player then
          return true
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:addPlayerMark(player, "hx__shuangzhen_can", 1)
    room:addPlayerMark(player, "hx__shuangzhen_count", 1)
    if player:getMark("hx__shuangzhen2") > 0 then
      for _, p in ipairs(room:getAlivePlayers()) do
        if p:getMark("@hx__shuangzhen_mark") > 0 then
          room:drawCards(p,2,hx__shuangzhen.name)
          room:recover{who = p, num = 1, reason = hx__shuangzhen.name}
          room:setPlayerMark(p, "@hx__shuangzhen_mark", 0)
        end
      end
      room:setPlayerMark(player, "hx__shuangzhen2", 0)
    else
      local target = room:askToChoosePlayers(player, {
        targets = room:getAlivePlayers(),
        min_num = 1,
        max_num = 1,
        prompt = "#hx__shuangzhen-choose",
        skill_name = hx__shuangzhen.name,
        cancelable = false,
      })
      local to = target[1]
      room:addPlayerMark(to, "@hx__shuangzhen_mark", 1)
      room:addPlayerMark(player, "@hx__shuangzhen_mark", 1)
      room:recover{who = player, num = 1, reason = hx__shuangzhen.name}
      room:recover{who = to, num = 1, reason = hx__shuangzhen.name}
      room:changeShield(player,1)
      room:changeShield(to,1)
      room:addPlayerMark(player, "hx__shuangzhen2", 1)
    end
  end,
})
hx__shuangzhen:addEffect(fk.EnterDying, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(hx__shuangzhen.name) and player:getMark("hx__shuangzhen_count") < 2 then
      if player:getMark("hx__shuangzhen2") == 0 then
        if player:getMark("@hx__shuangzhen_mark") == 0 then
          return true
        end
      else
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:addPlayerMark(player, "hx__shuangzhen_can", 1)
    room:addPlayerMark(player, "hx__shuangzhen_count", 1)
    if player:getMark("hx__shuangzhen2") > 0 then
      for _, p in ipairs(room:getAlivePlayers()) do
        if p:getMark("@hx__shuangzhen_mark") > 0 then
          room:drawCards(p,2,hx__shuangzhen.name)
          room:recover{who = p, num = 1, reason = hx__shuangzhen.name}
          room:setPlayerMark(p, "@hx__shuangzhen_mark", 0)
        end
      end
      room:setPlayerMark(player, "hx__shuangzhen2", 0)
    else
      local target = room:askToChoosePlayers(player, {
        targets = room:getAlivePlayers(),
        min_num = 1,
        max_num = 1,
        prompt = "#hx__shuangzhen-choose",
        skill_name = hx__shuangzhen.name,
        cancelable = false,
      })
      local to = target[1]
      room:addPlayerMark(to, "@hx__shuangzhen_mark", 1)
      room:addPlayerMark(player, "@hx__shuangzhen_mark", 1)
      room:recover{who = player, num = 1, reason = hx__shuangzhen.name}
      room:recover{who = to, num = 1, reason = hx__shuangzhen.name}
      room:changeShield(player,1)
      room:changeShield(to,1)
      room:addPlayerMark(player, "hx__shuangzhen2", 1)
    end
  end,
})
hx__shuangzhen:addEffect(fk.TargetConfirmed, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(hx__shuangzhen.name) and player:getMark("hx__shuangzhen_count") < 2 then
      if player:getMark("hx__shuangzhen2") == 0 then
        if player:getMark("@hx__shuangzhen_mark") == 0 then
          if target == player then
            return true
          end
        end
      else
        if target == player then
          return true
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:addPlayerMark(player, "hx__shuangzhen_can", 1)
    room:addPlayerMark(player, "hx__shuangzhen_count", 1)
    if player:getMark("hx__shuangzhen2") > 0 then
      for _, p in ipairs(room:getAlivePlayers()) do
        if p:getMark("@hx__shuangzhen_mark") > 0 then
          room:drawCards(p,2,hx__shuangzhen.name)
          room:recover{who = p, num = 1, reason = hx__shuangzhen.name}
          room:setPlayerMark(p, "@hx__shuangzhen_mark", 0)
        end
      end
      room:setPlayerMark(player, "hx__shuangzhen2", 0)
    else
      local target = room:askToChoosePlayers(player, {
        targets = room:getAlivePlayers(),
        min_num = 1,
        max_num = 1,
        prompt = "#hx__shuangzhen-choose",
        skill_name = hx__shuangzhen.name,
        cancelable = false,
      })
      local to = target[1]
      room:addPlayerMark(to, "@hx__shuangzhen_mark", 1)
      room:addPlayerMark(player, "@hx__shuangzhen_mark", 1)
      room:recover{who = player, num = 1, reason = hx__shuangzhen.name}
      room:recover{who = to, num = 1, reason = hx__shuangzhen.name}
      room:changeShield(player,1)
      room:changeShield(to,1)
      room:addPlayerMark(player, "hx__shuangzhen2", 1)
    end
  end,
})

hx__shuangzhen:addEffect(fk.TurnEnd, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    if  player:hasSkill(hx__shuangzhen.name) then
      if event == fk.TurnEnd and player:getMark("hx__shuangzhen_can") > 0 then
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:addPlayerMark(player, "hx__shuangzhen1", 1)
    if player:getMark("hx__shuangzhen_can") == 1 then
      if player:getMark("hx__shuangzhen2") == 1 then
        room:setPlayerMark(player, "hx__shuangzhen_can", 2)
        room:setPlayerMark(player, "hx__shuangzhen2", 2)
        room:setPlayerMark(player, "hx__shuangzhen1", 0)
      end
    end
    if player:getMark("hx__shuangzhen_can") >= 2 and player:getMark("hx__shuangzhen1") == 2 then
      room:setPlayerMark(player, "hx__shuangzhen2", 0)
      for _, p in ipairs(room:getAlivePlayers()) do
        if p:getMark("@hx__shuangzhen_mark") > 0 then
          room:setPlayerMark(p, "@hx__shuangzhen_mark", 0)
        end
      end
      room:setPlayerMark(player, "hx__shuangzhen_can", 0)
      room:setPlayerMark(player, "hx__shuangzhen_count", 0)
      room:setPlayerMark(player, "hx__shuangzhen1", 0)
    end
  end,
})

return hx__shuangzhen